/*
 * @Author: your name
 * @Date: 2020-07-25 22:04:31
 * @LastEditTime: 2020-08-07 10:50:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \stroage_server\platform\region_mgnt\region_mgnt.c
 */
#include "region_mgnt.h"
#include "xeSqlite.h"

#if 0
/**
 * @Date: 2020-08-05 16:02:23
 * @Description: 
 * @Return: 
 */
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,  
        char *outbuf, size_t outlen) {  
    iconv_t cd;  
    char **pin = &inbuf;  
    char **pout = &outbuf;  
  
    cd = iconv_open(to_charset, from_charset);  
    if (cd == 0)  
        return -1;  
    memset(outbuf, 0, outlen);  
    if (iconv(cd, pin, &inlen, pout, &outlen) == -1)  
        return -1;  
    iconv_close(cd);  
    *pout = '\0';  
  
    return 0;  
}
#endif
/**
 * @Date: 2020-07-28 09:28:19
 * @Description: 
 * @Return: 
 */
void region_setup(void *data)
{
    int retval = 0;
    cJSON *root;
    int ret_flag = 0;
    char errReason[HISOME_LEN_512]={'\0'};
    MyHttpPack* req =(MyHttpPack*)data;
    e_opt_type sqlite_opttype = E_OPT_INVALID;
    t_region_res region_in = {0};

    t_region_query_result region_result={0};
    region_result.p_vector =  vector_create(t_region_res);
    __check_return_point(region_result.p_vector,ret_flag,"malloc p_vector failed",region_exit);

    root=cJSON_Parse(req->httpReq.hm->body.p);
    if (!root) {
        sprintf(errReason,"客户端发送数据有误");
        ret_flag=1;
        PRTLOG_ERR("Error before: [%s]",cJSON_GetErrorPtr());
        goto region_exit;
    }else{
        char value[HISOME_LEN_256];
        __prase_cjson_string(value,ret_flag,"SID",region_exit);
            strcpy(region_in.sid,value);
        __prase_cjson_string(value,ret_flag,"opttype",region_exit);
            sscanf(value,"%d",&region_in.opttype);
        switch (region_in.opttype){
            case HTTP_REQ_REGION_NEW:
                __prase_cjson_string(value,ret_flag,"parent_indexcode",region_exit);
                    strcpy(region_in.parent_indexcode,value);
                __prase_cjson_string(value,ret_flag,"region_name",region_exit);
                    strcpy(region_in.region_name,value);
                cJSON_GetObjectValue(value,sizeof(value),root,"GTS_SN");
                strcpy(region_in.collector_gts,value);
                break;
            case HTTP_REQ_REGION_DELETE:
                __prase_cjson_string(value,ret_flag,"indexcode",region_exit);
                strcpy(region_in.indexcode,value);
                break;
            case HTTP_REQ_REGION_MODIY:
                __prase_cjson_string(value,ret_flag,"indexcode",region_exit);
                strcpy(region_in.indexcode,value);
                __prase_cjson_string(value,ret_flag,"region_name",region_exit);
                    strcpy(region_in.region_name,value);
                __prase_cjson_string(value,ret_flag,"GTS_SN",region_exit);
                strcpy(region_in.collector_gts,value);
                break;
            case HTTP_REQ_REGION_QUERY:
                __prase_cjson_string(value,ret_flag,"parent_indexcode",region_exit);
                    strcpy(region_in.parent_indexcode,value);
                break;
            default:
                memset(value,0,sizeof(value));
                __check_return_cstring(value,ret_flag,"opttype invalid",region_exit); 
        }
    }
    switch (region_in.opttype){
        case HTTP_REQ_REGION_NEW:
            // 生成uuid 
            tb_uuid4_make_cstr(region_in.indexcode,NULL);
            sqlite_opttype = E_INSERT_RECORD;
            break;
        case HTTP_REQ_REGION_DELETE:
            sqlite_opttype = E_DELETE_RECORD;
            break;
        case HTTP_REQ_REGION_MODIY:
            sqlite_opttype = E_UPDATE_RECORD;
            break;
        case HTTP_REQ_REGION_QUERY:
            sqlite_opttype = E_QUERY_RECORD;
            break;
    }
    retval = region_mgnt_opt(sqlite_opttype,&region_in,&region_result);
    if(retval < 0){
        ret_flag = 1;
        sprintf(errReason,"db exec failed");
    }
region_exit:
    {
        cJSON *rsp,*rspHeader,*rspArr,*rspData;

        rsp=cJSON_CreateArray();
        rspHeader = cJSON_CreateObject();
        rspData = cJSON_CreateObject();

        cJSON_AddStringToObject(rspHeader,"method","POST");
        cJSON_AddItemToObject(rspHeader, "data", rspArr=cJSON_CreateArray());

        if(ret_flag){
            cJSON_AddStringToObject(rspHeader,"result","FAIL");
            cJSON_AddStringToObject(rspData,"reason",errReason);
            cJSON_AddItemToArray(rspArr,rspData);
        }else{
            cJSON_AddStringToObject(rspHeader,"result","SUCCESS");
            if(region_in.opttype == HTTP_REQ_REGION_NEW){
                cJSON_AddStringToObject(rspData,"name",region_in.region_name);
                cJSON_AddStringToObject(rspData,"indexcode",region_in.indexcode);
                cJSON_AddItemToArray(rspArr,rspData);
            }else if(region_in.opttype == HTTP_REQ_REGION_QUERY){
                vector_iter iter;
                vector_t *c = region_result.p_vector;
                c->tmp_cursor = 0;
                for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c)) {
                    t_region_res *tt = vector_iter_valuep(c, iter, t_region_res);
                    cJSON *each_record =cJSON_CreateObject();
                    cJSON_AddStringToObject(each_record,"region_name", tt->region_name);
                    cJSON_AddStringToObject(each_record,"indexcode",tt->indexcode);
                    cJSON_AddStringToObject(each_record,"GTS_SN",tt->collector_gts);
                    cJSON_AddItemToArray(rspArr,each_record);
                }
            }
        }
        
        cJSON_AddItemToArray(rsp,rspHeader);

        com_httpPack(req->httpReq.nc,rsp);
        cJSON_Delete(rsp);  
    }
    if(root)
        cJSON_Delete(root);
    if(region_result.p_vector)
        vector_destroy(region_result.p_vector);
}

int  dw_get_region(cJSON *json_array_parent,char *parent_indexcode);
void region_query_all(void *data)
{
    cJSON *root;
    int ret_flag = 0;
    char errReason[HISOME_LEN_512]={'\0'};
    MyHttpPack* req =(MyHttpPack*)data;

    t_region_res region_in = {0};

    root=cJSON_Parse(req->httpReq.hm->body.p);
    if (!root) {
        sprintf(errReason,"客户端发送数据有误");
        ret_flag=1;
        PRTLOG_ERR("Error before: [%s]",cJSON_GetErrorPtr());
        goto region_query_exit;
    }else{
        char value[HISOME_LEN_256];
        __prase_cjson_string(value,ret_flag,"SID",region_query_exit);
            strcpy(region_in.sid,value);
        __prase_cjson_string(value,ret_flag,"opttype",region_query_exit);
            sscanf(value,"%d",&region_in.opttype);
        switch (region_in.opttype){
            case HTTP_REQ_REGION_QUERY_ALL:
                __prase_cjson_string(value,ret_flag,"parent_indexcode",region_query_exit);
                    strcpy(region_in.parent_indexcode,value);
                break;
            default:
                memset(value,0,sizeof(value));
                __check_return_cstring(value,ret_flag,"opttype invalid",region_query_exit); 
        }
    }

region_query_exit:
    {
        cJSON *rsp,*rspHeader,*rspArr,*rspData;
        rsp=cJSON_CreateArray();
        rspHeader = cJSON_CreateObject();
        rspData = cJSON_CreateObject();

        cJSON_AddStringToObject(rspHeader,"method","POST");
        cJSON_AddItemToObject(rspHeader, "data", rspArr=cJSON_CreateArray());

        if(ret_flag){
            cJSON_AddStringToObject(rspHeader,"result","FAIL");
            cJSON_AddStringToObject(rspData,"reason",errReason);
            cJSON_AddItemToArray(rspArr,rspData);
        }else{
            cJSON_AddStringToObject(rspHeader,"result","SUCCESS");
            dw_get_region(rspArr,region_in.parent_indexcode);
        }
        cJSON_AddItemToArray(rsp,rspHeader);
        com_httpPack(req->httpReq.nc,rsp);
        cJSON_Delete(rsp);  
    }
    if(root)
        cJSON_Delete(root);
}

int  dw_get_region(cJSON *json_array_parent,char *parent_indexcode)
{
	int retval = -1;
    t_region_res region_in = {0};
    t_region_query_result region_result={0};
    region_result.p_vector =  vector_create(t_region_res);
    if(!region_result.p_vector){
        STD_print("malloc failed\n");
        xeLOG_ERR("malloc failed\n");
        return -1;
    }
    strcpy(region_in.parent_indexcode,parent_indexcode);

    retval = region_mgnt_opt(E_QUERY_RECORD,&region_in,&region_result);
    if(retval < 0){
        return -1;
    }

    vector_iter iter;
    vector_t *c = region_result.p_vector;
    c->tmp_cursor = 0;
    for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c)) {
        t_region_res *tt = vector_iter_valuep(c, iter, t_region_res);
        cJSON *each_record =cJSON_CreateObject();
        cJSON *rsoArr;
        cJSON_AddStringToObject(each_record,"region_name", tt->region_name);
        cJSON_AddStringToObject(each_record,"indexcode",tt->indexcode);
        cJSON_AddStringToObject(each_record,"GTS_SN",tt->collector_gts);
        cJSON_AddItemToObject(each_record, "childlist", rsoArr=cJSON_CreateArray());
            dw_get_region(rsoArr,tt->indexcode);
        cJSON_AddItemToArray(json_array_parent,each_record);
    }
    if(region_result.p_vector)
        vector_destroy(region_result.p_vector);
    
    return 0;

}
